package com.lovo.test.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//存钱界面
public class StorePanel extends JPanel implements ActionListener {
    private ATM frame;

    private JButton returnBut;//返回按钮
    private JButton confirmBut;//创建确认按钮对象
    private JTextField storeField;//金额输入文本框
    private JLabel remindLabel;//提示标签框

    public StorePanel(ATM frame){
        this.frame=frame;
        //空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);

        //返回按钮,返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(350,280,100,50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认存款金额
        confirmBut = new JButton("确认");
        confirmBut.setBounds(450,280,100,50);
        this.add(confirmBut);
        confirmBut.addActionListener(this);

        //以下是文本框
        storeField = new JTextField();//存钱文本框
        storeField.setBounds(350,170,100,20);
        this.add(storeField);

        //以下是标签
        remindLabel = new JLabel("请输入存款金额：");
        remindLabel.setBounds(250,170,100,20);
        add(remindLabel);


    }

    //事件监听器
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==returnBut){
            frame.switchPanel("菜单");
        }else if (e.getSource()==confirmBut){//存款确认跳转
            deposit();//调用存款函数
            JOptionPane.showMessageDialog(this,"存款成功");

        }

    }
    //存款函数
    public  void deposit(){
       User user= AtmMysq.getById(frame.getUserName().getId());
       int storeMoney= Integer.parseInt(storeField.getText());//获取存入金额，并转成int类型
        int Money=user.getMoney();
        Money+=storeMoney;
        user.setMoney(Money);
        AtmMysq.modify1(user);
    }
}
